package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Academy;
import com.example.server.web.domain.Course;
import com.example.server.web.domain.dto.CourseParamDto;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.CourseMapper;
import com.example.server.web.service.AcademyService;
import com.example.server.web.service.CourseService;
import com.example.server.web.utils.MyObjectUtils;
import com.example.server.web.utils.UUIDKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private AcademyService academyService;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void add(Course course) {
        //先查询学院名字
        Academy academy = academyService.getById(course.getAcademyId());
        String academyName = academy.getAcademyName();
        //设置学院名字
        course.setAcademyName(academyName);
        //新增前校验
        addCheck(course);
        if (StringUtils.isBlank(course.getCode())){
            course.setCode(UUIDKeyGenerator.generateCode("CR",true));
        }
        //新增
        int insert = courseMapper.insert(course);
        if (insert < 1) {
            throw new CustomException("新增失败");
        }
    }

    @Override
    public void modify(Course course) {
        //先查询学院名字
        Academy academy = academyService.getById(course.getAcademyId());
        MyObjectUtils.throwExIfNull(academy,"该学院不存在！");
        String academyName = academy.getAcademyName();
        //设置学院名字
        course.setAcademyName(academyName);
        //修改
        int update = courseMapper.updateById(course);
        if (update < 1) {
            throw new CustomException("新增失败");
        }
    }

    @Override
    public void delete(Long id) {
        int delete = courseMapper.deleteById(id);
        if (delete < 1) {
            throw new CustomException("删除失败");
        }
    }

    @Override
    public Map getPage(CourseParamDto courseParamDto, PageQueryDto pageQueryDto) {
        Map<String, Object> map = new HashMap<>();
        //取出分页数据
        Integer page = pageQueryDto.getPage();
        Integer limit = pageQueryDto.getLimit();
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        //课程名字模糊查询
        if (StringUtils.isNotBlank(courseParamDto.getCourseName())) {
            wrapper.like(Course::getCourseName, courseParamDto.getCourseName());
        }
        //学院id精确查询
        if (courseParamDto.getAcademyId() != null) {
            wrapper.eq(Course::getAcademyId,courseParamDto.getAcademyId());
        }
        //状态精确查询
        if (StringUtils.isNotBlank(courseParamDto.getStatus())){
            wrapper.eq(Course::getStatus,courseParamDto.getStatus());
        }
        Page<Course> list = this.page(new Page<>(page,limit),wrapper);
        map.put("list",list.getRecords());
        map.put("total",list.getTotal());
        return map;
    }

    /**
     * 新增之前校验填写的信息
     * @param course
     */
    private void addCheck(Course course) {
        if (StringUtils.isBlank(course.getCode())) return;
        //查询是否存在重复学院代码
        LambdaQueryWrapper<Course> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Course::getCode, course.getCode());
        Course two = courseMapper.selectOne(wrapper2);
        if(two!=null){
            throw  new CustomException("该课程代码已存在，请重新换一个！");
        }
    }
}
